<?php

session_start();

require_once "db.php";
	$seatColor = null;
if (isset($_SESSION['uisadmin']) && $_SESSION['uisadmin'] >-1) //user is logged 
{
	include "header.php";


	//rezervirane
	if (isset($_POST["reserve"])) {

		echo "gbg";
		if(isset( $_POST['seaters']))
		{
			echo $_POST["brseats"];
		$aChecks = $_POST['seaters'];
			
		  if(empty($aChecks)) 
		  {
		    echo("You didn't select any seats.");
		  } 
		  else
		  {
		    $N = count($aChecks);
		 
		    echo("You selected $N seat(s): ");
		    for($i=0; $i < $N; $i++)
		    {	if(isset($_POST["seaters[".$i."]"]))
					echo $i;

		     if(isset($aChecks[$i]))
		      echo ( " 1 ");
		    }
		  }
	
		}
				
	}
	//4as nenujen (zasega)
	//echo 'Hello ' . htmlspecialchars($_GET["resid"]) . '!';
	if(!isset($_GET["prid"])){
		$_GET["prid"]=$_POST["hid"];

	}
	echo 'Hello ' . htmlspecialchars($_GET["prid"]) . '!';

			$projid=$_GET["prid"];
			$quer=" select  p.hall_id as hall_id, h.seat_count from projection p join halls h on (h.id=p.hall_id) where p.id=".$_GET["prid"];
			$res= db_query($quer);
		 	$r=db_get_row($res);
		 	echo "hall_id=".$r["hall_id"]."mesta:".$r["seat_count"];
		 	$id=$r["hall_id"];
		 	$seats=$r["seat_count"];
		 	$brmr=$seats/10;

?>
<form method="post" action='<?php echo $_SERVER["PHP_SELF"]; ?>'> 
	<table border='1'>
<?php
	for($i=1;$i<=$seats;$i++){
		if($i%$brmr==$brmr/2){
			
			?>
			


			<?php echo "<td>  </td>";
							?>
			<?php
		}
		elseif($i%$brmr==1){
			?>
			<?php echo "<tr><td><form action=\"".$_SERVER["PHP_SELF"]."\" method='post'>
									<input type=\"hidden\" name=\"".$i."\" value=".$i.">
									
									<input type=\"submit\" value=".$i.">
									</form></td>";
							?>
			<?php
		}
		elseif($i%$brmr==0){
			?>
			<?php echo "<td><form action=\"".$_SERVER["PHP_SELF"]."\" method='post'>
									<input type=\"hidden\" name=\"".$i."\" value=".$i.">
									
									<input type=\"submit\" value=".$i.">
									</form></td></tr>";
							?>
			<?php
		}
		
		else{
			?>
			<?php echo "<td><form action=\"".$_SERVER["PHP_SELF"]."\" method='post'>
									<input type=\"hidden\" name=\"".$i."\" value=".$i.">
									<input type=\"hidden\" name=\"hid\" value=".$projid.">
	<input type=\"hidden\" name=\"brseats\" value=".$seats.">
									<input type=\"submit\" value=".$i.">
									</form></td>";
							?>
			<?php
		}




	}
	echo "<input type=\"hidden\" name=\"hid\" value=".$projid.">";
	echo "<input type=\"hidden\" name=\"brseats\" value=".$seats.">";
	echo "</table> 	<input type=\"submit\" name=\"reserve\" value=\"reserve\"/> </form>";



?>
<?php //////////////////////////////////////////////////////////////////////////////////

	$seatcount="Select seat_count from halls where id=".$hallid;
	$res1=db_query($seatcount);
	$seatres = db_get_row($res1);

	for($i=1;$i<=$seatres["seat_count"];$i++){
		$takenseats="select seat,projection_id from reservation where projection_id=".$pr_id." and seat=".$i;
		$restaken=db_query($takenseats);
		$seatsrow=db_get_row($restaken);
		//echo $i;
		if (isset($seatsrow['seat'])){
			echo $seatsrow['seat']."  ";
			$taken[$seatsrow['seat']]="";
		}
		else $taken[$i]="<input type='checkbox' name=".$i.">";


	}
	//$seatarr=$restaken->fetch_array();
	//echo $seatarr["seat"];
	// 	foreach($seatarr["seat"] as $key=>$value)
		// 			echo $key." i ".$value;
		//  	for($i=1;$i<=$seatres["seat_count"];$i++){
		//  		if()

			//  	}
	if (isset($_POST['Reserve']))
	{	for($i=1;$i<=$seatres["seat_count"];$i++)
		if(isset($_POST[$i])){
		echo $i;
		echo $_SESSION['username'];
		$quer="insert into reservation values(\"".$_SESSION['username']."\",".$hallid.",".$i.")";
		$dbrec=db_query($quer)
		or pretty_die("problem reading table2".mysql_error());
		$taken[$i]="";
	}
	//else $taken[$i]="<input type='checkbox' name=".$i.">";
	//var_dump($_POST,$taken);

	}
	echo "<form method='post' action=".$_SERVER["PHP_SELF"].">";
	echo "<table border='1'>";
	for($i=1;$i<=$seatres["seat_count"];$i++)
	{

		if($i%10==1){
			echo"<tr><td>".$i."<br/>".$taken[$i]."</td>";
	}
	elseif($i%10==0){
	echo"<td>".$i."<br/>".$taken[$i]."</td></tr>";
	}
	elseif($i%10==5){
	echo"<td>".$i."<br/>".$taken[$i]."</td><td> &nbsp &nbsp &nbsp</td>";
	}
	else{
	echo"<td>".$i."<br/>".$taken[$i]."</td>";
	}
}
echo "</table>";
echo "<input type='submit' name='Reserve' value='Reserve'/>";
echo "</form>";
}
?>
<?php

	include "footer.php";

?>